import {createRouter, createWebHistory} from 'vue-router'
import {userInfoStore} from "@/stores/counter.js";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            component: () => import('../views/AdminView.vue'),
            redirect: "/home",
            meta: {
                title: '首页'
            },
            children: [
                {
                    path: 'home',
                    name: 'home',
                    component: () => import('../views/admin/HomeView.vue'),
                    meta: {
                        title: '首页'
                    }
                },
                {
                    path: 'menu',
                    name: 'Menu',
                    component: () => import('../views/admin/MenuView.vue'),
                    meta: {
                        title: '菜单管理'
                    }
                },
                {
                    path: 'permission',
                    name: 'Permission',
                    component: () => import('../views/admin/PermissionView.vue'),
                    meta: {
                        title: '权限管理'
                    }
                },
                {
                    path: 'role',
                    name: 'Role',
                    component: () => import('../views/admin/RoleView.vue'),
                    meta: {
                        title: '角色管理'
                    }
                },
                {
                    path: 'userinfo',
                    name: 'Userinfo',
                    component: () => import('../views/admin/UserInfoView.vue'),
                },
                {
                    path: 'depart',
                    name: 'Depart',
                    component: () => import('../views/admin/DepartView.vue'),
                },
                {
                    path: 'vendor',
                    name: 'Vendor',
                    component: () => import('../views/cloud/VendorView.vue'),
                },
                {
                    path: 'cloud/platform',
                    name: 'CloudPlatform',
                    component: () => import('../views/cloud/CloudPlatformView.vue'),
                },
                {
                    path: 'pool',
                    name: 'Pool',
                    component: () => import('../views/cloud/PoolView.vue'),
                },
                {
                    path: 'host',
                    name: 'Host',
                    component: () => import('../views/cloud/HostView.vue'),
                },
                {
                    path: 'vm',
                    name: 'Vm',
                    component: () => import('../views/cloud/VmView.vue'),
                },
                {
                    path: 'flavor',
                    name: 'Flavor',
                    component: () => import('../views/manage/FlavorView.vue'),
                },
                {
                    path: 'os',
                    name: 'OS',
                    component: () => import('../views/manage/OperationSystemView.vue'),
                },
                {
                    path: 'order',
                    name: 'Order',
                    component: () => import('../views/manage/OrderView.vue')
                },
                {
                    path: 'apply/vm',
                    name: 'ApplyVm',
                    component: () => import('../views/manage/ApplyVmView.vue'),
                },
                {
                    path: 'apply/net',
                    name: 'ApplyNet',
                    component: () => import('../views/manage/ApplyNetView.vue'),
                },

            ]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue'),
        },
    ],
})
router.beforeEach((to, from, next) => {
    if (to.name === "login") {
        next()
        return
    }
    const info = userInfoStore()
    if (!info.userToken) {
        router.push({name: "login"})
        return
    }
    // console.log(info.userMenuNameList)
    // console.log(to.name)
    if (to.name !== 'home' & info.userMenuNameList.indexOf(to.name) === -1) {
        next({"name": 'home'})
        return
    }
    next()
})
export default router
